class EventServer:
    def __init__(self):
        self.receivers = {}
        
    def register(self, cmd, func):
        if not self.receivers.has_key(cmd):
            self.receivers[cmd] = []
            
        self.receivers[cmd].append(func)
        
    def unregister(self, cmd, func):
        if self.receivers.has_key(cmd) and func in self.receivers[cmd]:
            self.receivers[cmd].remove(func)
            
    def unregisterObject(self, obj):
        for cmd, funcList in self.receivers.iteritems():
            removeList = []
            for func in funcList:
                if hasattr(func,'im_self') and func.im_self == obj:
                    removeList.append(func)
            for func in removeList:
                funcList.remove(func)
        
    def pull(self, cmd, args):
        if self.receivers.has_key(cmd):
            for func in self.receivers[cmd]:
                func(cmd,args)
                
    def netPull(self, cmd, args):
        from system.networkServer import networkServer
        if networkServer.send("event",(cmd,args)):
            return True
        else:
            self.pull(cmd,args)
            return False
        
    def distPull(self, cmd, args):
        from system.networkServer import networkServer
        networkServer.send("revent",(cmd,args))
        
    def serverPull(self, cmd, args):
        from system.networkServer import networkServer
        networkServer.send("sevent",(cmd,args))

eventServer = EventServer()
